package com.gwh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gwh.common.BaseContext;
import com.gwh.common.R;
import com.gwh.entity.Dish;
import com.gwh.entity.ShoppingCart;
import com.gwh.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart cart = shoppingCartService.add(shoppingCart);
        return R.success(cart);
    }

    /**
     * 菜品或套餐的取消
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    private R<String> delete(@RequestBody ShoppingCart shoppingCart){
        shoppingCartService.delete(shoppingCart);
        return R.success("删除失败");
    }

    @DeleteMapping("/clean")
    private R<String> clean(){
        shoppingCartService.clean();
        return R.success("清空购物车成功");
    }

}
